
package com.plsjavaweb.ui.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.plsjavaweb.core.ReportsCore;
import com.plsjavaweb.core.EmployeeSeatCore;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.CSReportsPOJO;
import com.plsjavaweb.pojo.EmployeeSeatPOJO;
import com.plsjavaweb.pojo.ProjectPOJO;
import com.plsjavaweb.ui.controler.modelview.ProjectResourceEmployeePOJO;

/**
 * Servlet implementation class Home
 */
@WebServlet("/Home")
public class Home extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Logger log = Logger.getLogger(Home.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Home() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info(LoggingConstants.entryMessage);
		HttpSession session = request.getSession(true);		
		// select the displayed dashboard depending on the role
		if (session != null) {
			String floor = "3F";
			String locationCode = "PIC";
			int employeeID = (Integer) session.getAttribute("employeeId");	
			//int employeeID = Integer.parseInt(employeeIDStr);
			String role = (String) session.getAttribute("role");
			
			EmployeeSeatCore empSeatCore = new EmployeeSeatCore();
			List<EmployeeSeatPOJO> empSeats = empSeatCore.getEmployeeSeatsByBldgByFloor("PIC", "3F");
			request.setAttribute("employeeSeats", empSeats);
		
			if (role.equalsIgnoreCase("Manager")) {
				log.info("Manager entered");
				//created by me
				
				
				if (!role.equalsIgnoreCase("MANAGER")) {
					response.sendRedirect("Home");
					return;
				}
				int managerId = (Integer) session.getAttribute("employeeId");
				ReportsCore pmReportsCore = new ReportsCore();
				List<ProjectPOJO> projects = new ArrayList<ProjectPOJO>();

				projects = pmReportsCore.getProjectSeatByManager(managerId);
				request.setAttribute("projects", projects);
				session.setAttribute("projects", projects);

				
				
					// get the projects for the manager
				

				

			} else if (role.equalsIgnoreCase("Admin")) {
				log.info("Admin entered.");
				// get the report for the managers project seat allocation
				if (!role.equalsIgnoreCase("ADMIN")) {
					response.sendRedirect("Home");
					return;
				}
				ReportsCore csReportsCore = new ReportsCore();
				CSReportsPOJO reports = new CSReportsPOJO();
				List<ProjectPOJO> projects = new ArrayList<ProjectPOJO>();
				reports = csReportsCore.getTotalSeatReportsPerFloor(floor);
				projects = csReportsCore.getProjectSeat();

				session.setAttribute("reports", reports);
				session.setAttribute("projects", projects);

				request.setAttribute("reports", reports);
				request.setAttribute("projects", projects);

				
				//set dummy requests for managing seat
				request.setAttribute("newseatrequestcount", 2);
				request.setAttribute("seattransferrequestcount", 4);
				request.setAttribute("seatrigodonrequestcount", 0);
				request.setAttribute("seatapprovalcount", 1);
				request.setAttribute("seattransferapprovalcount", 1);
				
				
			} else {
				log.info("Ordinary User entered.");
				
				// forward the default employee dashboard
			}
			
			
			request.getRequestDispatcher("WEB-INF/dashboard.jsp").forward(
					request, response);
			
			log.info(LoggingConstants.exitMessage);
		} else {
			response.sendRedirect("index.jsp");
			return;
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//doGet(request, response);
	}

}

